package com.metaera.app.ui.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.metaera.app.Constants;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.LoginVerPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.ui.core.BaseRequestActivity;

import com.metaera.app.utils.ActivityNavigateManager;
import com.metaera.app.utils.validated.EmptyRule;
import com.metaera.app.utils.validated.Validated;

public class ResetPasswordActivity extends BaseRequestActivity {

    public static Intent newInstance(
            Activity activity,
            int type,
            CharSequence phoneNumber,
            CharSequence areaCode,
            CharSequence code) {
        Intent intent = new Intent(activity, ResetPasswordActivity.class);
        intent.putExtra("PAGE_TYPE", type);
        intent.putExtra(Constants.PHONE_NUMBER, phoneNumber.toString());
        intent.putExtra(Constants.EXTRA_AREA_CODE, areaCode.toString());
        intent.putExtra(Constants.PHONE_VER_CODE, code.toString());
        return intent;
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        Button btn_ok = findViewById(R.id.btn_ok);
        EditText et_one_pwd = findViewById(R.id.et_one_pwd);
        EditText et_confirm_pwd = findViewById(R.id.et_confirm_pwd);
        View reset_pwd_back = findViewById(R.id.reset_pwd_back);

        int pageType = getIntent().getIntExtra("PAGE_TYPE", -1);

        Validated.add(btn_ok, new EmptyRule(et_one_pwd), new EmptyRule(et_confirm_pwd));

        reset_pwd_back.setOnClickListener(v -> finish());

        btn_ok.setOnClickListener(v -> {
            LoginVerPresenter presenter = new LoginVerPresenter();
            presenter.setModelAndView(new BasicNetworkRequestView<>(data -> ActivityNavigateManager.range(this, LoginActivity.class)));

            if (pageType == VerifyCodeActivity.FORGET_TYPE) {
                presenter.forgotPassword(
                        getStringExtra(Constants.PHONE_VER_CODE),
                        getStringExtra(Constants.PHONE_NUMBER),
                        getStringExtra(Constants.EXTRA_AREA_CODE),
                        et_one_pwd.getText().toString(),
                        et_confirm_pwd.getText().toString()
                );
            } else {
                presenter.resetPassword(
                        getStringExtra(Constants.PHONE_VER_CODE),
                        getStringExtra(Constants.PHONE_NUMBER),
                        getStringExtra(Constants.EXTRA_AREA_CODE),
                        et_one_pwd.getText().toString(),
                        et_confirm_pwd.getText().toString()
                );
            }

        });
    }

}
